package com.polaris.live.ui.set

import android.os.Bundle
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.databinding.ActivityDmaBinding
import com.polaris.live.resp.event.DmaEvent
import com.polaris.live.utils.DmaUtils
import com.polaris.live.utils.getBooleanOrString
import org.greenrobot.eventbus.EventBus

/**
 * DmaActivity
 *
 * @author Created by lucas on 2024/2/26 11:09
 */
class DmaActivity : BaseVbActivity<BaseViewModel, ActivityDmaBinding>() {

    private val forceShow by lazy {
        intent.getBooleanOrString("forceShow", false)
    }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setNavigationBarColor(R.color.color_f8f9fa)
    }

    override fun initView(savedInstanceState: Bundle?) {
        val dmaSelected = DmaUtils.getDmaSelected()
        if (dmaSelected != null) {
            mBinding.switchConsent.isChecked = dmaSelected
        }

        mBinding.bvTitle.setOnLeftClientListener {
            onDmaConfirm(true)
        }
        mBinding.tvConfirm.setOnSingleClickListener {
            onDmaConfirm(true)
        }
        mBinding.tvCancel.setOnSingleClickListener {
            onDmaConfirm(mBinding.switchConsent.isChecked)
        }
    }

    private fun onDmaConfirm(agree: Boolean) {
        DmaUtils.setDmaUserSelected(agree)

        EventBus.getDefault().post(DmaEvent(agree))

        finish()
    }

    override fun onBackPressed() {
        if (!forceShow) {
            onDmaConfirm(true)
        }

        super.onBackPressed()
    }
}